package com.aj.activity;

import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

import com.aj.activity.adapter.MoreFamilyAdapter;
import com.aj.application.LoveHomeApplication;
import com.aj.databases.dao.AccountDao;
import com.aj.databases.dao.FamilyDao;
import com.aj.databases.dao.HomeDao;
import com.aj.databases.dao.UserDao;
import com.aj.databases.entry.Account;
import com.aj.databases.entry.Family;
import com.aj.databases.entry.Home;
import com.aj.databases.entry.User;
import com.aj.http.AsynchronizationHttpRequest.IHttpCallBack;
import com.aj.http.request.HomeRequest;
import com.aj.http.response.BaseRespond;
import com.aj.http.response.Common;
import com.aj.http.response.HomeResponse;
import com.aj.utils.Contants;

public class MoreFamilyActivity extends BaseActivity {
	private GridView familygridview;
	private HomeDao homeDao;
	private FamilyDao familyDao;
	private List<Home> homeList;
	private MoreFamilyAdapter moreFamilyAdapter;
	private static final String TAG = MoreFamilyActivity.class.getName();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_more_family);
		initView();
		setActionBarTitle("更多家人");
	}

	@Override
	protected void onResume() {
		super.onResume();
		showProgressDialog();
		if (LoveHomeApplication.token != null
				&& LoveHomeApplication.token.trim().length() > 0) {
			AccountDao accountDao = new AccountDao(MoreFamilyActivity.this);
			UserDao userDao = new UserDao(MoreFamilyActivity.this);
			Account account = accountDao.getByToken(LoveHomeApplication.token);
			if (account != null) {
				User user = userDao.getUserByUserId(account
						.getLast_used_user_id());
				if (user != null) {
					new HomeRequest().excute(MoreFamilyActivity.this,
							user.getUser_id(), "50", "0", new IHttpCallBack() {
								@Override
								public void success(String result) {
									try {
										HomeResponse homeResponse = new HomeResponse(
												new JSONObject(result));
										dimissProgressDialog();
										if (homeResponse != null) {
											Common common = homeResponse
													.getCommon();
											if (common != null) {
												if (common
														.getReturnCode()
														.trim()
														.equals(BaseRespond.SUCCESS_RESULT_CODE_STRING)) {
													homeList = homeResponse
															.getHomeList();
													for (Home home : homeList) {
														home = homeDao
																.insert(home);
														if (home.getFamilyList() != null) {
															for (Family family : home
																	.getFamilyList()) {
																family = familyDao
																		.insert(family);
															}
														}
													}
													List<Family> familyList = familyDao
															.queryFamilyExistsMine();
													if (familyList != null
															&& familyList
																	.size() > 0) {
														moreFamilyAdapter = new MoreFamilyAdapter(
																MoreFamilyActivity.this,
																familyList);
														familygridview
																.setAdapter(moreFamilyAdapter);
														ItemClick itemClick = new ItemClick();
														familygridview
																.setOnItemClickListener(itemClick);
													}
												} else {
													Log.e(TAG,
															"message:"
																	+ common.getErrorMsg());
												}
											}
										}

									} catch (JSONException e) {
									}
								}

								@Override
								public void fail(String result) {
									dimissProgressDialog();
									try {
										Common common = new Common(
												new JSONObject(result));
										if (common != null) {
											Log.e(TAG,
													"message:"
															+ common.getErrorMsg());
										}
									} catch (JSONException e) {
										e.printStackTrace();
									}
								}
							});
				}
			}
		}

	}

	private void initView() {
		familygridview = (GridView) findViewById(R.id.familygridview);
		familygridview.setAdapter(moreFamilyAdapter);
		homeDao = new HomeDao(this);
		familyDao = new FamilyDao(this);
	}

	class ItemClick implements OnItemClickListener {
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			Intent intent = new Intent(Contants.FAMILY_PHOTO_ACTIVITY_ACTION);
			startActivity(intent);
		}
	}
}
